#!/usr/bin/env bash

INSTANCE=""
OUTFILE=playlist.json
TOKEN=""
while getopts "i:o:t:" arg; do
	case $arg in
		i)
			INSTANCE=${OPTARG}
			;;
		o)
			OUTFILE=${OPTARG}
			;;
		t)
			TOKEN=${OPTARG}
			;;
	esac
done
shift $((OPTIND-1))

if [ -z "${INSTANCE}" ]; then
    echo No instance given
	exit 1
fi

if [ -z "${TOKEN}" ]; then
    echo No token given
	exit 1
fi

if [ "$(host ${INSTANCE}|grep "not found"|wc -l)" -lt 0 ]; then
	echo Instance ${INSTANCE} has no DNS entry
	exit 1
fi

if [ "$(curl -s -X 'GET' https://${INSTANCE}/api/v1/instance/nodeinfo/2.0/ -H 'accept: application/json' |jq -r '.software.name')" != "funkwhale" ]; then
	echo Instance ${INSTANCE} seems not to be a funkwhale instance
	exit 1
fi

nexturl="https://${INSTANCE}/api/v1/playlists/"

while [ "${nexturl}" != "null" ]; do
	answer="$(curl -s -X 'GET' -H 'Authorization: Bearer '${TOKEN} -H 'accept: application/json' ${nexturl})"
	nexturl="$(echo ${answer}|jq -r '.next')"
	echo ${answer}|jq -r '.results[].id'
done|xargs -n1 -P 4 ./delete_single_playlist.sh -i ${INSTANCE} -t ${TOKEN} -p 

